/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.examples.fraclite.statecontrol;

import org.objectweb.fractal.api.control.BindingController;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.api.control.LifeCycleController;

public class ClientImpl implements Runnable, BindingController, LifeCycleController {

  // Bindings
  protected static final String serviceItfName = "s";
  protected Service service;
  
  protected static final String[] listItf = new String[] {serviceItfName};
  
  Boolean started = Boolean.FALSE;
  
  // Runnable
  @Override
  public void run () {
    for(int i = 0; i<5; i++){
      service.print("hello world");
    }
  }

  // BindingController
  @Override
  public String[] listFc () {
    return listItf;
  }

  @Override
  public Object lookupFc (final String cItf) {
    if (cItf.equals(serviceItfName)) {
      return service;
    }
    return null;
  }

  @Override
  public void bindFc (final String cItf, final Object sItf) {
    if (cItf.equals(serviceItfName)) {
      service = (Service)sItf;
    }
  }

  @Override
  public void unbindFc (final String cItf) {
    if (cItf.equals(serviceItfName)) {
      service = null;
    }
  }

  @Override
  public String getFcState() {
    return started?LifeCycleController.STARTED:LifeCycleController.STOPPED;
  }

  @Override
  public void startFc() throws IllegalLifeCycleException {
    if(!started){
      service.setHeader(" > ");
      started = true;
    }
  }

  @Override
  public void stopFc() throws IllegalLifeCycleException {
    started = false;
  }
}
